package com.gpsnav.util;
import javax.microedition.io.*;
import java.io.*;
import com.gpsnav.bo.BCachingException;
import net.rim.device.api.servicebook.*;

/**
 *
 * @author mbastian
 */
public class BCachingUtil {
    private static String connectionUID = null;

    public static String testLogin(String username, String hashword) throws BCachingException {
           
        String url = buildUrl(username, hashword, "a=login");
        Communication com1 = new Communication();
        int code = com1.httpConnection_GET(url);
        if (code != HttpConnection.HTTP_OK) {
            throw new BCachingException(com1.getResponse());
        } 

        return com1.getResponse();
//        String data = getViaHttpConnection(url);
//        return data;
    }
           
    public static String getNearCaches(String username, String hashword, double lat, double lon, int maxDistance, int numCaches) throws BCachingException {
//        String request = "a=find&lat="+lat+"&lon="+lon+"&maxdistance="+maxDistance + "&maxcount=" + numCaches;
        String request = "a=nearest&desc=long&logs=3&lat="+lat+"&lon="+lon+"&maxdistance="+maxDistance + "&maxcount=" + numCaches;
        String url = buildUrl(username, hashword, request);

        Communication com1 = new Communication();
        int code = com1.httpConnection_GET(url);
        if (code != HttpConnection.HTTP_OK) {
            throw new BCachingException(com1.getResponse());
        } 

        return com1.getResponse();
    }
    
           
    public static String buildUrl(String username, String hashword, String request) {
        java.util.Date date = java.util.Calendar.getInstance().getTime();
        String qs = "u=" + Communication.getURLencode(username) + "&" + request + "&time=" + date.getTime();

        qs += "&sig=" + Communication.getURLencode(encodeMd5Base64(qs + hashword));   

        String url = "http://www.bcaching.com/api/q.ashx?"+qs;
//        String url = "http://test.bcaching.com/api/q.ashx?"+qs;

//System.out.println("______url = "+url);
        return url;
    }
           
             
    public static String encodePassword(String username, String password)
            //throws Exception
    {
        return encodeMd5Base64(password + username);
    }
    
    public static String encodeMd5Base64(String s)
            //throws Exception
    {
        byte[] buf = s.getBytes();
        net.rim.device.api.crypto.MD5Digest md = new net.rim.device.api.crypto.MD5Digest();
        md.update(buf, 0, buf.length);
        buf = new byte[16];
        md.getDigest(buf, 0, true);       
        return base64Encode(buf);
    }
    
    private static char[] map1 = new char[64];
    static {
        int  i  = 0;
        for ( char c = 'A'; c <= 'Z'; c++ ) { map1[i++] = c; }
        for ( char c = 'a'; c <= 'z'; c++ ) { map1[i++] = c; }
        for ( char c = '0'; c <= '9'; c++ ) { map1[i++] = c; }
        map1[i++] = '+';
        map1[i++] = '/';
    }
 
    public static String base64Encode( byte[] in ) {
        int iLen = in.length;
        int oDataLen = ( iLen * 4 + 2 ) / 3;// output length without padding
        int oLen = ( ( iLen + 2 ) / 3 ) * 4;// output length including padding
        char[] out = new char[oLen];
        int ip = 0;
        int op = 0;
        int i0, i1, i2, o0, o1, o2, o3;
        while ( ip < iLen ) {
            i0 = in[ip++] & 0xff;
            i1 = ip < iLen ? in[ip++] & 0xff : 0;
            i2 = ip < iLen ? in[ip++] & 0xff : 0;
            o0 = i0 >>> 2;
            o1 = ( ( i0 & 3 ) << 4 ) | ( i1 >>> 4 );
            o2 = ( ( i1 & 0xf ) << 2 ) | ( i2 >>> 6 );
            o3 = i2 & 0x3F;
            out[op++] = map1[o0];
            out[op++] = map1[o1];
            out[op] = op < oDataLen ? map1[o2] : '=';
            op++;
            out[op] = op < oDataLen ? map1[o3] : '=';
            op++;
        }
        return new String( out );
    }
}
